package com.patentbackend.patentmanage.repository;

import com.patentbackend.patentmanage.entity.Patent;
import com.patentbackend.patentmanage.entity.PatentStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface PatentRepository extends JpaRepository<Patent, Long> {
    Page<Patent> findAll(Pageable pageable);

    @Query("SELECT p FROM Patent p WHERE " +
            "p.title LIKE %:keyword% OR " +
            "p.patentNumber LIKE %:keyword% OR " +
            "p.organization LIKE %:keyword% OR " +
            "p.applicant LIKE %:keyword% OR " +
            "p.inventor LIKE %:keyword%")
    Page<Patent> search(@Param("keyword") String keyword, Pageable pageable);

    @Query("SELECT p FROM Patent p WHERE " +
            "p.annualFeePaid = false AND " +
            "p.annualFeeDueDate BETWEEN :today AND :tenDaysLater")
    List<Patent> findAnnualFeeDueSoon(@Param("today") Date today, @Param("tenDaysLater") Date tenDaysLater);

    List<Patent> findByStatus(PatentStatus status);
}